import { Observable } from 'rxjs';
import { LeaveRequest } from './../../../domain/leave-request';
import { FormsModule, FormGroup, FormBuilder, Validators } from '@angular/forms';
import { LeaveRequestStateService, LeaveTest } from './../../../service/leave-request-state.service';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { SelectorMatcher } from '@angular/compiler';
import { ManagerService } from './../../../service/manager.service';
import { User } from 'src/app/domain/user';
import { AuthService } from '../../../service/auth.service';
/*

@Author:赵明洋

@Date: 2018-12-12

@员工请假功能实现

@Last Modified time: 2018-12-16
*/
@Component({
  selector: 'app-leave-request',
  templateUrl: './leave-request.component.html',
  styleUrls: ['./leave-request.component.css']
})
export class LeaveRequestComponent implements OnInit {
  private user: User;
  sid = AuthService.getUserId();

  // formModule:FormGroup;  
  leaveTest: LeaveTest = {
    user_id: this.sid,
    start_time: '1',
    end_time: '',
    reason: '',
    state: 2,
    duration: 0,
    type: 0
  };
  leaveRequest: LeaveRequest = {
    id: 0,
    user_id: 1000,
    start_time: '1',
    end_time: '',
    reason: '',
    state: 2,
    duration: 0,
    type: 1,
    suggestion: "",
  };

  constructor(private router: Router, private routeInfo: ActivatedRoute, private leaverequestservice: LeaveRequestStateService, private managerService: ManagerService) {
    this.user = {
      id: null,
      user_name: null,
      name: null,
      age: null,
      sex: null,
      department: null, //部门
      position: null, //职务
      annual_leave_length: null, //年假上限
      remain_annual_leave: null, //剩余年假
    };

  }
  id: string;
  start_time: string;
  end_time: string;
  reason: string;
  state: number;
  duration: number;
  type: number;
  morereason: string;

  ngOnInit() {
    let leaveID = this.routeInfo.snapshot.params['id'];

    if (leaveID != 0) {
      this.leaverequestservice.getLeaveRequest(leaveID).subscribe(
        data => {
          this.leaveRequest = data[0];
          //console.log(data[0].reason);
          this.start_time = this.leaveRequest.start_time;
          this.end_time = this.leaveRequest.end_time;
          this.reason = this.leaveRequest.reason;
          this.type = this.leaveRequest.type;
          this.duration = this.leaveRequest.duration;

        }
      )

    }

    this.getUser();
  }

  getUser() {

    var id = AuthService.getUserId();
    console.log(id, this.sid);
    if (id == 0) {
      alert("请先登录");
    }
    else {
      this.managerService.getUser(id)
        .subscribe(user => { this.user = user[0]; console.log(user[0]) });
    }

  }

  cancel() //取消按钮函数
  {
    this.router.navigateByUrl('/staff/leaverequeststate');
  }

  save(): void {
    //利用表单数据绑定把我想要的数据全部更新一次


    this.leaveTest.start_time = this.start_time;
    this.leaveTest.end_time = this.end_time;
    this.leaveTest.reason = this.reason;
    this.leaveTest.type = this.type;
    this.leaveTest.duration = this.duration;

    var tempType = this.leaveRequest.type;
    var tempDuration = this.leaveRequest.duration;

    this.leaveRequest.start_time = this.start_time;
    this.leaveRequest.end_time = this.end_time;
    this.leaveRequest.reason = this.reason;
    this.leaveRequest.type = this.type;
    this.leaveRequest.duration = this.duration;
    this.leaveTest.state = 2;
    this.leaveRequest.state = 2;

    //如果是点击修改进入的界面
    if (this.leaveRequest.id != 0) {
      //用于多重异步的判定
      let flag = 0;

      //进入不是年假，离开不是年假
      if (tempType != 3 && this.type != 3) {
        this.leaverequestservice.upDateLeaveRequest(this.leaveRequest)
          .subscribe(data => this.router.navigateByUrl('/staff/leaverequeststate'));
      }

      //进入是年假，离开是年假
      if (tempType == 3 && this.type == 3) {
        //恢复当前用户的年假
        this.user.remain_annual_leave = Number(this.user.remain_annual_leave) + Number(tempDuration);

        //判定年假剩余
        if (this.duration > this.user.remain_annual_leave) {
          alert("剩余年假不足，请重新调整...\n" + "当前年假为：" + this.user.remain_annual_leave);
          this.router.navigateByUrl('/staff/leaverequeststate');
          return;
        }

        //如果没改变年假天数
        if (tempDuration == this.duration) {
          this.leaverequestservice.upDateLeaveRequest(this.leaveRequest)
            .subscribe(data => this.router.navigateByUrl('/staff/leaverequeststate'));
          return;
        } else {
          this.user.remain_annual_leave = Number(this.user.remain_annual_leave) - this.duration;
          this.managerService.updateUserleave(this.user)
            .subscribe(() => {
              if (++flag == 2) this.router.navigateByUrl('/staff/leaverequeststate');
            });
          this.leaverequestservice.upDateLeaveRequest(this.leaveRequest)
            .subscribe(() => {
              if (++flag == 2) this.router.navigateByUrl('/staff/leaverequeststate');
            });
          return;
        }
      }

      //进入是年假，离开不是年假
      if (tempType == 3 && this.type != 3) {
        this.user.remain_annual_leave = Number(this.user.remain_annual_leave) + Number(tempDuration);
        this.managerService.updateUserleave(this.user)
          .subscribe(() => {
            if (++flag == 2) this.router.navigateByUrl('/staff/leaverequeststate');
          });
        this.leaverequestservice.upDateLeaveRequest(this.leaveRequest)
          .subscribe(() => {
            if (++flag == 2) this.router.navigateByUrl('/staff/leaverequeststate');
          });
        return;
      }
      //进入不是年假，离开是年假
      if (tempType != 3 && this.type == 3) {
        //判定年假剩余
        if (this.duration > this.user.remain_annual_leave) {
          alert("剩余年假不足，请重新调整...\n" + "当前年假为：" + this.user.remain_annual_leave);
          this.router.navigateByUrl('/staff/leaverequeststate');
          return;
        }
        this.user.remain_annual_leave = Number(this.user.remain_annual_leave) - Number(this.duration);
        this.managerService.updateUserleave(this.user)
          .subscribe(() => {
            if (++flag == 2) this.router.navigateByUrl('/staff/leaverequeststate');
          });
        this.leaverequestservice.upDateLeaveRequest(this.leaveRequest)
          .subscribe(() => {
            if (++flag == 2) this.router.navigateByUrl('/staff/leaverequeststate');
          });
        return;
      }

    }
    //如果是添加新请假进入的界面
    else {
      //扣除年假时长
      if (this.type == 3 && this.duration > this.user.remain_annual_leave) {
        alert("剩余年假不足，请重新调整...\n" + "当前年假为：" + this.user.remain_annual_leave);
        return;
      }
      if (this.type == 3) {
        this.user.remain_annual_leave -= this.duration;
        this.leaverequestservice.addLeaveRequest(this.leaveTest)
          .subscribe(data => console.log(data));
        this.managerService.updateUserleave(this.user)
          .subscribe(data => this.router.navigateByUrl('/staff/leaverequeststate'));
      } else {
        this.leaverequestservice.addLeaveRequest(this.leaveTest)
          .subscribe(data => this.router.navigateByUrl('/staff/leaverequeststate'));

      }

    }
  }
}
